//
//  LDBOssRecJobFinderCell.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/6/6.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

import UIKit
@objc
class LDBossRecJobFinderCell: LDBaseRecFinderCell {
    
    let margin_V = LDMargin_V //竖直间隔
    let headImageWidth:CGFloat = 50 //kuan
    let BtnHeight:CGFloat = 20
    
    //上部
    var usertitleLab = UILabel()
    /** Button */
    var salaryLab = UILabel()
    var cityBtn = UIButton()
    var experienceBtn = UIButton()
    var eduBtn = UIButton()
    
    var divideLineView1 = UIImageView.setupDivideLine_H(LDFrame.ScreenWidth - Margin_V*10)
    
    //中部
    var headImageView = UIImageView()
    var jobfinderNameLab = UILabel()
    var jobfinderStatus = UILabel()
    
//    var divideLineView2 = UIImageView.setupDivideLine_H(LDFrame.ScreenWidth - Margin_V*10)
    //下部
    var jobfinderDescLab:UILabel!
    var jobfinderVoiceStatus:UIButton = {
        
        let btn = UIButton()
        btn.setTitleColor(LDFontColor.Black, forState: .Normal)
        btn.setTitleColor(LDFontColor.Black, forState: .Normal)
        btn.setTitle("  文字简历", forState: .Normal)
        btn.setTitle("  语音简历", forState: .Selected)
        btn.titleLabel?.font = LDFont.Font14
        
        btn.setImage(UIImage.init(named: "common_text"), forState: .Normal)
        btn.setImage(UIImage.init(named: "common_voice"), forState: .Selected)
        
        return btn
    }()
    var locationBtn = UIButton()
    
    //jobmodels对象初始化
    dynamic var models:DDJobfinderDetail? {
        
        didSet{
            
            if models != nil {
                
                updateContentSub()
                updateFrames()
            }
        }
    }
    //frame
    private let btnMargin:CGFloat = 10
    let bottomLine = UIView()
    
    //MARK: - OVERRIDE
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = UIColor.whiteColor()
        //布局
        setDefaultContent()
        setupFrame()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func updateContentSub(){
        
        if models == nil {
            return
        }
        
        let jobFinderInfo:DDJobfinderDetail = self.models!
        
        //上
        usertitleLab.text = jobFinderInfo.expectjobtitle
        salaryLab.text = "¥\(jobFinderInfo.expectsalary)"
        
        
        let cityStr = jobFinderInfo.expectcity ?? "期望城市"
        let experienceStr = jobFinderInfo.experience ?? "经验"
        let eduStr = jobFinderInfo.edu ?? "学历"
        
        cityBtn.setTitle(cityStr, forState: .Normal)
        experienceBtn.setTitle(experienceStr, forState: .Normal)
        eduBtn.setTitle(eduStr, forState: .Normal)
        
        //中
        let api = LDCommentAPI.getPicApi(jobFinderInfo.logo ?? "")
        headImageView.sd_setImageWithURL(NSURL.init(string: api), placeholderImage: UIImage(named: "default_header.png"))
        let employername = jobFinderInfo.name != nil ? jobFinderInfo.name : "姓名"
        let sex = jobFinderInfo.sex != nil ? jobFinderInfo.sex:"性别"
        
        //
        let nameLabStr = "\(employername) | \(sex) "
//        let attr = NSMutableAttributedString(string: nameLabStr)
//        let range = NSRange(location: 0, length: employername.characters.count)
//        attr.addAttributes([NSForegroundColorAttributeName:LDFontColor.Green], range: range)
//        jobfinderNameLab.attributedText = attr
        
        jobfinderNameLab.text = nameLabStr
        jobfinderStatus.text = jobFinderInfo.status != nil ? jobFinderInfo.status:"工作状态"
        
        //下
        jobfinderDescLab.text = jobFinderInfo.mystrengthstext
        
        
        let isAudio = jobFinderInfo.isresumeaudio ?? "0"
        if isAudio == "1" {
            
            jobfinderVoiceStatus.selected = true
        } else {
            
            jobfinderVoiceStatus.selected = false
        }
        
    }
    
    func updateFrames() {
        
        dispatch_async(dispatch_get_global_queue(0, 0)) { [weak self] in
            
            
            let textsize = CGSizeMake(120, 13)
            
            let cityStr = self!.models!.expectcity ?? "期望城市"
            let experienceStr = self!.models!.experience ?? "经验"
            let eduStr = self!.models!.edu ?? "学历"
            
            let width1 = cityStr.boundingRectWithSize(textsize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName:LDFont.Font13], context: nil).size.width
            let width2 = experienceStr.boundingRectWithSize(textsize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName:LDFont.Font13], context: nil).size.width
            let width3 = eduStr.boundingRectWithSize(textsize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName:LDFont.Font13], context: nil).size.width
            
            dispatch_async(dispatch_get_main_queue(), {
                
                
                self!.cityBtn.snp_updateConstraints { (make) in
                    
                    make.width.equalTo(width1 + 30)
                }
                
                self!.experienceBtn.snp_updateConstraints { (make) in
                    make.width.equalTo(width2 + 30)
                    
                }
                
                self!.eduBtn.snp_updateConstraints { (make) in
                    make.width.equalTo(width3 + 30)
                }
                
            })
        }
        
    }
    
    // MARK: - init 布局
    func setDefaultContent(){
        
        
        usertitleLab = UILabel.setHeadTitleLab("Title")
        
        //分割线1
        
        salaryLab = UILabel.setDetailsLab("", fontSize: 13, titleColor: LDFontColor.Red,Alignment: nil)
        
        cityBtn = UIButton.setLoactionBtn()
        experienceBtn = UIButton.setExperienceBtn()
        eduBtn = UIButton.setEduBtn()
        locationBtn = UIButton.setDistanceBtn()
        
        headImageView = UIImageView.setDefaultGoundImg("default_header_master.png", radius: headImageWidth/2)
        
        jobfinderNameLab.textColor = LDFontColor.Black
        jobfinderNameLab.font = LDFont.Font14
        
        jobfinderStatus.textColor = UIColor.grayColor()
        jobfinderStatus.font = LDFont.Font13
        
        jobfinderDescLab = UILabel.setDetailsLab("描述", fontSize: 12, titleColor: LDFontColor.LightGray, Alignment: .Left)
        jobfinderDescLab.textColor = UIColor.grayColor()
        jobfinderDescLab.font = LDFont.Font12
        
        //
//        jobfinderVoiceStatus = UIImageView.init(image: UIImage.init(named: "common_text"))
        
        
        locationBtn.titleLabel?.font = LDFont.Font12
        locationBtn.hidden = true
        
        contentView.addSubview(usertitleLab)
        contentView.addSubview(divideLineView1)
        contentView.addSubview(salaryLab)
        contentView.addSubview(cityBtn)
        contentView.addSubview(experienceBtn)
        contentView.addSubview(eduBtn)
        contentView.addSubview(headImageView)
        contentView.addSubview(jobfinderNameLab)
        contentView.addSubview(jobfinderStatus)
//        contentView.addSubview(divideLineView2)
//        contentView.addSubview(jobfinderDescLab)
        contentView.addSubview(jobfinderVoiceStatus)
//        contentView.addSubview(locationBtn)
    }
    
    //MARK - 自动布局
    func setupFrame(){
        
        //头部
        headImageView.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo(margin_V*3)
            make.left.equalTo(margin_V*5)
            make.height.width.equalTo(headImageWidth)
        })
        
        jobfinderNameLab.snp_remakeConstraints(closure: { (make) in
            
            make.centerY.equalTo(headImageView)
            make.left.equalTo(headImageView.snp_right).offset(LDMargin_H*2)
            make.height.equalTo(15)
        })
        
        divideLineView1.snp_remakeConstraints(closure: { (make) in
            
            make.top.equalTo(headImageView.snp_bottom).offset(LDMargin_H)
            make.height.equalTo(0.5)
            make.left.equalTo(LDMargin_V*2)
            make.right.equalTo(-LDMargin_V*2)
        })
        
        //----------------------------------
        usertitleLab.snp_makeConstraints(closure: { (make) in
            
            
            make.top.equalTo(divideLineView1.snp_bottom).offset(LDMargin_V*2)
            make.left.equalTo(headImageView)
            make.height.equalTo(20)
            
        })
        
        salaryLab.snp_makeConstraints(closure: { (make) in
            
            make.centerY.equalTo(usertitleLab)
            make.right.equalTo(-LDMargin_V*3)
            make.height.equalTo(20)
        })
        
        //
        jobfinderVoiceStatus.snp_makeConstraints { (make) in
            
            make.left.equalTo(usertitleLab).offset(-LDMargin_H-2)
            make.top.equalTo(usertitleLab.snp_bottom).offset(LDMargin_V*2)
            make.size.equalTo(CGSizeMake(90, 28))
        }
        
        jobfinderStatus.snp_makeConstraints { (make) in
            
            make.left.equalTo(jobfinderVoiceStatus.snp_right).offset(LDMargin_V*2)
            make.centerY.height.equalTo(jobfinderVoiceStatus)
        }
        
        //下部-------------------
        cityBtn.snp_makeConstraints(closure: { (make) in
            
            make.top.equalTo(jobfinderVoiceStatus.snp_bottom).offset(LDMargin_V*1.5)
            make.left.equalTo(usertitleLab).offset(-3)
            make.height.equalTo(15)
        })
        
        experienceBtn.snp_makeConstraints { (make) in
            
            make.top.centerY.height.equalTo(cityBtn)
            make.left.equalTo(cityBtn.snp_right).offset(LDMargin_V)
        }
        
        eduBtn.snp_makeConstraints { (make) in
            
            make.top.centerY.height.equalTo(experienceBtn)
            make.left.equalTo(experienceBtn.snp_right).offset(LDMargin_V)
        }
    }
}
